package com.tanger.im.service.contacts;

import com.tanger.common.exception.AbstractController;
import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.StringUtil;
import com.tanger.im.entity.ImUserContacts;
import com.tanger.im.entity.IMUserContactsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description 用户控制层
 * @Author Hchenbin
 * @Date 2023/12/25 9:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/tanger/api/test/user:contacts")
@Api(value = "IM:IM用户通讯录",tags="IM:IM用户通讯录")
public class IMUserContactsController extends AbstractController{

    @Autowired
    private IMUserContactsService service;

    @ApiImplicitParam(name = "userId", value = "当前IM用户ID", required = true, paramType = "query", dataType = "Long")
    @ApiOperation(value = "联系人列表查询")
    @GetMapping("/findAll")
    public ActionResult<List<IMUserContactsVO>> findAll(@RequestParam Long userId){
        if (StringUtil.isNull(userId)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return ActionResult.buildResultSuccess(service.findAll(userId));
    }

    @ApiImplicitParam(name = "id", value = "主键ID", required = true, paramType = "query", dataType = "Long")
    @ApiOperation(value = "好友详情")
    @GetMapping("/findOne")
    public ActionResult<IMUserContactsVO> findOne(@RequestParam Long id){
        if (StringUtil.isNull(id)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return ActionResult.buildResultSuccess(service.findOne(id));
    }

    @ApiOperation(value = "保存/添加好友")
    @PostMapping("/save")
    public ActionResult save(@RequestBody ImUserContacts contacts) {
        if (Objects.isNull(contacts)
                || StringUtil.isNull(contacts.getCronyId()) || StringUtil.isNull(contacts.getUserId())) {
            return ActionResult.buildResultError("参数缺失");
        }
        return service.save(contacts);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "isblack", value = "是否黑名单(1.是 0.否)", required = true, paramType = "query", dataType = "Integer")
    })
    @ApiOperation(value = "添加/解除黑名单")
    @PostMapping("/black")
    public ActionResult black(@RequestParam Long id, Integer isblack) {
        if (StringUtil.isNull(id) || StringUtil.isNull(isblack)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return service.black(id, isblack);
    }

    @ApiImplicitParam(name = "id", value = "主键ID", required = true, paramType = "query", dataType = "Long")
    @ApiOperation(value = "删除好友")
    @DeleteMapping("/delete")
    public ActionResult delete(@RequestParam Long id) {
        if (StringUtil.isNull(id) ) {
            return ActionResult.buildResultError("参数缺失");
        }
        return service.delete(id);
    }
}

